<template>
	<view class="content">
		<view class="mine-item">
			<image mode="aspectFill" :src="user.faceUrl?user.faceUrl:'/static/mine/user_photo.png'" class="mine-img">
			</image>
			<view class="mine-name" v-if="user.userName.length>0">{{user.userName}}</view>
			<button open-type="getPhoneNumber" hover-class="none" @getphonenumber="getPhoneNumber" class="user-name"
				v-else>立即登录</button>
		</view>
		<view class="info-block">
			<view class="info-item" @click="navigate('/pages/mine/profile')">
				<view class="left-item">
					<image src="../../static/mine/my-info.png" class="img">
					</image>
					<view class="title">我的资料</view>
				</view>
				<view class="iconfont icon-youjiantou"></view>
			</view>
			<view class="info-item" @click="navigate('/pages/my-idea/my-idea')">
				<view class="left-item">
					<image src="../../static/mine/my-advice.png" class="img">
					</image>
					<view class="title">我的建议</view>
				</view>
				<view class="iconfont icon-youjiantou"></view>
			</view>
			<view class="info-item" @click="logout">
				<view class="left-item">
					<image src="../../static/mine/exit.png" class="img1">
					</image>
					<view class="title">退出登录</view>
				</view>
				<view class="iconfont icon-youjiantou"></view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getStorage,
		setStorage,
		removeStorage
	} from '@/util/auth.js';
	import {
		loginApi
	} from '@/api/login.js'
	export default {
		data() {
			return {
				custom: null,
				topBgColor: 'rgba(0,0,0,0)',
				titleColor: '#fff',
				user: {
					userName: ''
				},
			}
		},
		methods: {
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			getPhoneNumber(e) {
				if (!e.detail.code) {
					this.showToast('用户取消了授权');
					return;
				}
				uni.showLoading({
					title: '提交中',
					mask: true,
				})
				const mobileCode = e.detail.code;
				wx.login({
					success: (login) => {
						const code = login.code;
						console.log(code)
						uni.showLoading({
							title: '加载中',
							mask: true
						})
						loginApi({
							code,
							mobileCode
						}).then((res) => {
							uni.hideLoading()
							const loginInfo = {
								token: res.token,
								...res.user
							};
							this.user = res.user
							setStorage('login', loginInfo, 2678400);
							this.showToast('登录成功');
						})
					}
				})
			},
			logout() {
				uni.showModal({
					title: '提示',
					content: `确定要退出重新登录吗？`,
					confirmColor: '#027BFD',
					success: (res) => {
						if (res.confirm) {
							removeStorage('login');
							uni.reLaunch({
								url: '/pages/home/home'
							})
						}
					}
				})
			},
		},
		onShow() {
			const loginInfo = getStorage('login');
			if (loginInfo) {
				this.user = loginInfo
			}
		},
		onLoad() {},
	}
</script>

<style lang="scss" scoped>
	page {
		background: #F5F5F5;
	}

	.content {
		background: #F5F5F5;
		width: 100%;

		.mine-item {
			background-color: #157EFE;
			display: flex;
			flex-direction: row;
			padding: 30rpx;
			height: 350rpx;
			align-items: center;
			margin-top: -60rpx;

			.mine-img {
				width: 160rpx;
				height: 160rpx;
				border-radius: 100%;
				margin-right: 30rpx;
				background: #F5F5F5;
			}

			.user-name {
				font-size: 36rpx;
				font-weight: bold;
				background: none;
				margin: 0;
				border-radius: 0;
				line-height: normal;
				padding: 0;
				color: #fff;

				&::after {
					border: none;
				}
			}

			.mine-name {
				color: #fff;
				font-weight: bold;
				font-size: 40rpx;
			}
		}

		.info-block {
			display: flex;
			flex-direction: column;
			background: #fff;
			border-radius: 60rpx;
			margin-top: -50rpx;
			height: auto;
			position: relative;
			padding: 30rpx;

			.info-item {
				height: 160rpx;
				box-shadow: 0 0 10px 0 #dedede;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				margin-top: 30rpx;
				align-items: center;
				border-radius: 20rpx;
				padding-left: 40rpx;

				.left-item {
					display: flex;
					flex-direction: row;
					align-items: center;

					.img {
						width: 43rpx;
						height: 42rpx;
						margin-right: 20rpx;
					}

					.img1 {
						width: 38rpx;
						height: 38rpx;
						margin-right: 20rpx;
					}

					.title {
						font-weight: bold;
					}
				}

				.iconfont {
					color: #9FA7B4;
					margin-right: 10rpx;
					font-size: 30rpx;
				}
			}
		}
	}
</style>